//
//  PPTimelineCommentObject.m
//  pickpic
//
//  Created by Jinxiao on 10/5/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPTimelineCommentObject.h"

@implementation PPTimelineCommentObject

+ (instancetype)commentForParameters:(NSDictionary *)parameters {
    PPTimelineCommentObject *comment = [[PPTimelineCommentObject alloc] init];

    comment.commentID = [parameters validNumberForKey:@"commentid"];
    comment.date = [NSDate dateWithTimeIntervalSince1970:[parameters validNumberForKey:@"createDate"].doubleValue/1000.0];
    comment.content = [parameters validStringForKey:@"content"];

    NSDictionary *author = [parameters validDictionaryForKey:@"author"];

    PPTimelineUserObject *sender = [PPTimelineUserObject userForParameters:author];
    comment.sender = sender;

    if([parameters validNumberForKey:@"parent"].integerValue != -1) {
        NSDictionary *authorTo = [parameters validDictionaryForKey:@"authorTo"];

        PPTimelineUserObject *receiver = [PPTimelineUserObject userForParameters:authorTo];
        comment.receiver = receiver;
    }

    return comment;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:self.commentID forKey:@"commendID"];
    [encoder encodeObject:self.sender forKey:@"sender"];
    [encoder encodeObject:self.receiver forKey:@"receiver"];
    [encoder encodeObject:self.date forKey:@"date"];
    [encoder encodeObject:self.content forKey:@"content"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    self = [super init];

    if(self) {
        self.commentID = [decoder decodeObjectForKey:@"commendID"];
        self.sender = [decoder decodeObjectForKey:@"sender"];
        self.receiver = [decoder decodeObjectForKey:@"receiver"];
        self.date = [decoder decodeObjectForKey:@"date"];
        self.content = [decoder decodeObjectForKey:@"content"];
    }

    return self;
}

@end
